#define SAMPLES 100

int high;
int low;
int ave;

int count = 0;
long currentSum = 0;
long lastSum = -1;

void setup() {
  Serial.begin(9600);  
}

void loop() {
  int value = analogRead(5);
  
  currentSum += value;
  count++;

  if (count == SAMPLES) {
    lastSum = currentSum;
    currentSum = 0;
    count = 0;
  }
  
  if (count == 0) {
    ave = value;
  } else if (lastSum < 0) {
    // first average calculation, don't have a lastSum
    ave = currentSum / count;
  } else {
    ave = (currentSum + lastSum) / (count + SAMPLES);
  }

  if (ave > high) {
    high = ave;
    Serial.println("");
    Serial.print("High: ");
    Serial.println(high);
  } else if (ave < low) {
    low = ave;
    Serial.println("");
    Serial.print("Low: ");
    Serial.println(low);
  }
  Serial.print("value: ");
  Serial.print(value);
  Serial.print(" ave: ");
  Serial.print(ave);
  Serial.print(" currentSum: ");
  Serial.print(currentSum);
  Serial.print(" lastSum: ");
  Serial.print(lastSum);
  Serial.print(" count: ");
  Serial.println(count);
  delay(200);
}
